package lib.classes;

import lib.Util;
import lib.gui.FriendsScreen;
import lib.gui.ProfileScreen;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class RichListNode implements FocusChangeListener, FieldChangeListener, KeypadListener
{
	private Profile _profile;
	NullField nf;
	private Background bkg;
	public VerticalFieldManager field;
	public FriendsScreen _view;
	public RichListNode(Profile p, FriendsScreen view)
	{
		_view = view;
		field = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		field.setPadding(2, 2, 2, 2);
		
		this._profile = p;
		HorizontalFieldManager r1 = new HorizontalFieldManager();
		r1.setPadding(3, 3, 3, 3);
		LabelField top = new LabelField(_profile.getAge() + " : " + (_profile.getGender().toUpperCase().substring(0,1)));
		r1.add(top);
		field.add(r1);
		
		HorizontalFieldManager r2 = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
		r2.setPadding(3, 3, 3, 3);
		EncodedImage _ei = Util.scaleImageToWidth(p.getImage(), 70);
        _ei = Util.scaleImageToHeight(_ei, 70);
		BitmapField dp = new BitmapField(_ei.getBitmap());
		r2.add(dp);
		LabelField name = new LabelField(_profile.getScreenname());
		name.setPadding(0, 0, 0, 3);
		r2.add(name);
		field.add(r2);
		nf = new NullField(Field.FOCUSABLE)
		{
			protected boolean navigationClick(int status, int time)
			{
				UiApplication.getUiApplication().invokeLater(new Runnable() {
	                public void run() 
	                {
	                	ProfileScreen pf = new ProfileScreen(_profile);
	                	_view.setSelectedProfile(_profile);
	                	UiApplication.getUiApplication().pushScreen( pf );
	                }
    			});
				return true;
			}
		};
		nf.setFocusListener(this);
		field.add(nf);
		field.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1, 1,1,1), Border.STYLE_SOLID));
	
	}
	
	public void fieldChanged(Field field, int context) 
	{
		
		
	}
	public void focusChanged(Field field, int eventType) 
	{
		if(field.equals(nf))
		{
			if(field.isFocus())
			{
				//setBkg(BackgroundFactory.createSolidBackground(Color.BEIGE));
				setBkg(BackgroundFactory.createSolidBackground(Color.CORNFLOWERBLUE));
			}else
			{
				setBkg(this.bkg);
			}
		}
		
	}
	
	public void setRealBkg(Background bkg) 
	{
		this.bkg = bkg;
		setBkg(bkg);
	}
	/**
	 * @param bkg the bkg to set
	 */
	public void setBkg(Background bkg) 
	{
		//this.bkg = bkg;
		field.setBackground(bkg);
	}
	/**
	 * @return the bkg
	 */
	public Background getBkg() {
		return bkg;
	}

}
